package function

import (
	"os"
	"path/filepath"

	"github.com/gin-gonic/gin"
)

func GetAbsPath(relativePath string) string {
	execPath, _ := os.Executable()
	path, _ := filepath.Split(execPath)
	if relativePath == "" {
		return ""
	}
	//兼容go run main.go模式，请在开发模式下使用，生产环境打包请注释掉
	if gin.Mode() == gin.DebugMode {
		path, _ = os.Getwd()
	}
	return filepath.Join(path, relativePath)
}

func GetCurrentAbsPath() string {
	execPath, _ := os.Executable()
	path, _ := filepath.Split(execPath)
	//兼容go run main.go模式，请在开发模式下使用，生产环境打包请注释掉
	if gin.Mode() == gin.DebugMode {
		path, _ = os.Getwd()
	}
	return path
}

func FileNotExistsAndCreate(path string) {
	_, err := os.Stat(path)
	if err != nil && os.IsNotExist(err) {
		_ = os.MkdirAll(path, 0755)
	}
}
